package pers.xiaojun.boot.module.system.controller.tenant;

import cn.hutool.core.bean.BeanUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import pers.xiaojun.boot.common.pojo.CommonResult;
import pers.xiaojun.boot.common.pojo.PageResult;
import pers.xiaojun.boot.common.util.BeanUtils;
import pers.xiaojun.boot.module.system.controller.tenant.vo.packages.TenantPackageQueryReqVO;
import pers.xiaojun.boot.module.system.controller.tenant.vo.packages.TenantPackageRespVO;
import pers.xiaojun.boot.module.system.controller.tenant.vo.packages.TenantPackageSaveReqVO;
import pers.xiaojun.boot.module.system.controller.tenant.vo.packages.TenantPackageSimpleRespVO;
import pers.xiaojun.boot.module.system.service.tenant.TenantPackageService;
import pers.xiaojun.boot.mybatis.core.convert.PageConvert;
import pers.xiaojun.boot.web.core.annotation.AdminApi;

import java.util.List;

/**
 * 租户套餐接口
 *
 * @author xiaojun
 * @since 2025-11-22
 */
@Tag(name = "租户套餐")
@AdminApi
@RestController
@RequestMapping("/system/tenant-package")
@RequiredArgsConstructor
public class TenantPackageController {
    
    private final TenantPackageService tenantPackageService;

    @PostMapping
    @PreAuthorize("@ss.hasPermission('system:tenant-package:create')")
    @Operation(summary = "创建租户套餐")
    public CommonResult<?> createTenantPackage(@RequestBody @Valid TenantPackageSaveReqVO createVO) {
        return CommonResult.success(tenantPackageService.saveTenantPackage(createVO).getId());
    }

    @PutMapping("/{id}")
    @PreAuthorize("@ss.hasPermission('system:tenant-package:update')")
    @Operation(summary = "更新租户套餐")
    @Parameter(name = "id", description = "租户套餐id", required = true)
    public CommonResult<?> updateTenantPackage(@PathVariable("id") Long tenantPackageId, @RequestBody @Valid TenantPackageSaveReqVO updateVO) {
        updateVO.setId(tenantPackageId);
        return CommonResult.success(tenantPackageService.updateTenantPackage(updateVO).getId());
    }

    @DeleteMapping("/{id}")
    @PreAuthorize("@ss.hasPermission('system:tenant-package:delete')")
    @Operation(summary = "删除租户套餐")
    @Parameter(name = "id", description = "租户套餐id", required = true)
    public CommonResult<?> deleteTenantPackage(@PathVariable("id") Long tenantPackageId) {
        tenantPackageService.removeTenantPackageById(tenantPackageId);
        return CommonResult.success();
    }

    @DeleteMapping("/batch/{ids}")
    @PreAuthorize("@ss.hasPermission('system:tenant-package:delete')")
    @Operation(summary = "删除一批租户套餐")
    @Parameter(name = "ids", description = "租户套餐id列表（使用,分隔）", required = true)
    public CommonResult<?> deleteTenantPackageBatch(@PathVariable("ids") List<Long> tenantPackageIds) {
        tenantPackageService.removeTenantPackageByIds(tenantPackageIds);
        return CommonResult.success();
    }

    @GetMapping("/{id}")
    @PreAuthorize("@ss.hasPermission('system:tenant-package:query')")
    @Operation(summary = "根据租户套餐Id获取租户套餐信息")
    @Parameter(name = "id", description = "租户套餐id", required = true)
    public CommonResult<TenantPackageRespVO> getTenantPackageById(@PathVariable("id") Long tenantPackageId) {
        return CommonResult.success(BeanUtil.toBean(tenantPackageService.getTenantPackageById(tenantPackageId), TenantPackageRespVO.class));
    }

    @GetMapping("/page")
    @PreAuthorize("@ss.hasPermission('system:tenant-package:query')")
    @Operation(summary = "获取租户套餐分页")
    public CommonResult<PageResult<TenantPackageRespVO>> getTenantPackageList(@Valid TenantPackageQueryReqVO queryVO) {
        return CommonResult.success(PageConvert.INSTANCE.toPageResult(tenantPackageService.listTenantPackagesPage(queryVO), TenantPackageRespVO.class));
    }

    @GetMapping("/simple-list")
    @PreAuthorize("@ss.hasPermission('system:tenant-package:query')")
    @Operation(summary = "获取简要租户套餐列表")
    public CommonResult<List<TenantPackageSimpleRespVO>> getSimpleTenantPackageList() {
        return CommonResult.success(BeanUtils.toBeans(tenantPackageService.list(), TenantPackageSimpleRespVO.class));
    }


}
